﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
using System.Web.UI.HtmlControls;
using System.IO;

namespace CRPTModularCorrousel.Menu
{
    [ParseChildren(true, "Items"), PersistChildren(true)]
    public class MenuControl:Control
    {
        private const string MODULAR_ID = "moodular";

        public MenuControl()
        {
            Items = new MenuItemTemplate();
        }

        [Browsable(true)]
        [Category("Behavior")]
        public string MenuId { get; set; }

        [Browsable(true)]
        [Category("Behavior")]
        public string MenuClass { get; set; }

        [Browsable(true)]
        [Category("Behavior")]
        public string MenuStyle { get; set; }

        protected override void OnInit(EventArgs e)
        {            
            base.OnInit(e);
            var bulletList = new HtmlGenericControl("ul");
            string pageName = Path.GetFileName(Page.AppRelativeVirtualPath);
            bulletList.Attributes.Add("Page", pageName);
            foreach (var item in Items)
            {
                item.PageName = pageName;
                item.InstantiateIn(bulletList);
            }
            bulletList.Attributes.Add("id", MenuId);
            if(!String.IsNullOrEmpty(MenuClass))
                bulletList.Attributes.Add("class", MenuClass);
            if(!String.IsNullOrEmpty(MenuStyle))
                bulletList.Attributes.Add("style", MenuStyle);
            this.Controls.Add(bulletList);
            Page.Header.Controls.Add(new LiteralControl("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + ResolveUrl("~/Styles/moodular.css") + "\" />"));           
        }

        [PersistenceMode(PersistenceMode.InnerProperty)]
        public MenuItemTemplate Items { get; set; }
    }
}
